package com.ldk.api;

import com.ldk.base.framework.util.DateUtil;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.Driver;
import com.ldk.framework.entity.Driverapp;
import com.ldk.framework.entity.Drivereducate;
import com.ldk.framework.entity.Driverstat;
import com.ldk.framework.entity.OperateArrive;
import com.ldk.framework.entity.OperateDepart;
import com.ldk.framework.entity.OperatePay;
import com.ldk.framework.entity.OrderCancel;
import com.ldk.framework.entity.OrderCreate;
import com.ldk.framework.entity.OrderMatch;
import com.ldk.framework.entity.PlatformDriver;
import com.ldk.framework.entity.RatedDriver;
import com.ldk.framework.entity.ServiceTrip;
import com.ldk.framework.entity.User;
import com.ldk.framework.entity.Vehicle;
import com.ldk.framework.entity.Vehicleinsurance;
import com.ldk.framework.entity.Vehicletotalmile;
import com.ldk.framework.enums.PayType;
import com.ldk.framework.service.ICompanyService;
import com.ldk.framework.service.ICompanyfareService;
import com.ldk.framework.service.ICompanypayService;
import com.ldk.framework.service.ICompanypermitService;
import com.ldk.framework.service.ICompanyserviceService;
import com.ldk.framework.service.ICompanystatService;
import com.ldk.framework.service.IDriverService;
import com.ldk.framework.service.IDriverappService;
import com.ldk.framework.service.IDrivereducateService;
import com.ldk.framework.service.IDriverstatService;
import com.ldk.framework.service.IOperateArriveService;
import com.ldk.framework.service.IOperateDepartService;
import com.ldk.framework.service.IOperateLoginService;
import com.ldk.framework.service.IOperateLogoutService;
import com.ldk.framework.service.IOperatePayService;
import com.ldk.framework.service.IOrderCancelService;
import com.ldk.framework.service.IOrderCreateService;
import com.ldk.framework.service.IOrderMatchService;
import com.ldk.framework.service.IPassengerService;
import com.ldk.framework.service.IPlatformDriverService;
import com.ldk.framework.service.IPositionDriverService;
import com.ldk.framework.service.IPositionVehicleService;
import com.ldk.framework.service.IRatedDriverService;
import com.ldk.framework.service.IRatedDriverpunishService;
import com.ldk.framework.service.IRatedPassengerService;
import com.ldk.framework.service.IRatedPassengercomplaintService;
import com.ldk.framework.service.IServiceTripService;
import com.ldk.framework.service.IUserService;
import com.ldk.framework.service.IVehicleService;
import com.ldk.framework.service.IVehicleinsuranceService;
import com.ldk.framework.service.IVehicletotalmileService;

import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ldk.base.framework.util.SystemUtil.generateRandom;
import static com.ldk.base.framework.util.SystemUtil.generateSalt;

/**
 * 交管数据上报
 *
 * @author mqy
 */
@Controller
@RequestMapping("/reportdata")
public class ReportDataController extends BaseController {

    @Autowired
    private ICompanypermitService companypermitService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private ICompanystatService companystatService;
    @Autowired
    private ICompanypayService companypayService;
    @Autowired
    private ICompanyfareService companyfareService;
    @Autowired
    private ICompanyserviceService companyserviceService;
    @Autowired
    private IVehicleService vehicleService;
    @Autowired
    private IServiceTripService serviceTripService;
    @Autowired
    private IVehicleinsuranceService vehicleinsuranceService;
    @Autowired
    private IVehicletotalmileService vehicletotalmileService;
    @Autowired
    private IPlatformDriverService platformDriverService;
    @Autowired
    private IDrivereducateService drivereducateService;
    @Autowired
    private IDriverappService driverappService;
    @Autowired
    private IDriverstatService driverstatService;
    @Autowired
    private IPassengerService passengerService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IOrderCreateService orderCreateService;
    @Autowired
    private IOrderCancelService orderCancelService;
    @Autowired
    private IOrderMatchService orderMatchService;
    @Autowired
    private IOperatePayService operatePayService;

    @Autowired
    private IOperateLoginService operateLoginService;
    @Autowired
    private IOperateLogoutService operateLogoutService;
    @Autowired
    private IOperateDepartService operateDepartService;
    @Autowired
    private IOperateArriveService operateArriveService;

    @Autowired
    private IPositionDriverService positionDriverService;
    @Autowired
    private IPositionVehicleService positionVehicleService;
    @Autowired
    private IRatedPassengerService ratedPassengerService;
    @Autowired
    private IRatedDriverpunishService ratedDriverpunishService;
    @Autowired
    private IRatedPassengercomplaintService ratedPassengercomplaintService;
    @Autowired
    private IRatedDriverService ratedDriverService;
    @Autowired
    private IDriverService driverService;
    @Autowired
    private TimePushTask timePushTask;
    @Autowired
    private TimerTaskManager timerTaskManager;

    //private String posturl = "http://czc.tyxxtb.com";
    private String posturl = "https://data.yycjg.com";

    /**
     * 上传数据至监管部门
     *
     * @param method
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploaddata", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String uploaddata(@RequestParam(value = "method", required = false, defaultValue = "1") String method) {
        String url = "";
        boolean success = false;
        if ("baseinfoCompany".equals(method)) {
            success = timePushTask.baseinfoCompany();
        } else if ("baseinfoCompanyStat".equals(method)) {
            success = timePushTask.baseinfoCompanyStat();
        } else if ("baseinfoCompanyPay".equals(method)) {
            success = timePushTask.baseinfoCompanyPay();
        } else if ("baseinfoCompanyService".equals(method)) {
            success = timePushTask.baseinfoCompanyService();
        } else if ("baseinfoCompanyPermit".equals(method)) {
            success = timePushTask.baseinfoCompanyPermit();
        } else if ("baseinfoCompanyFare".equals(method)) {
            success = timePushTask.baseinfoCompanyFare();
        } else if ("baseinfoVehicle".equals(method)) {
            success = timePushTask.baseinfoVehicle();
        } else if ("baseinfoVehicleInsurance".equals(method)) {
            success = timePushTask.baseinfoVehicleInsurance();
        } else if ("baseinfoVehicleTotalMile".equals(method)) {
            success = timePushTask.baseinfoVehicleTotalMile();
        } else if ("baseinfoDriver".equals(method)) {
            success = timePushTask.baseinfoDriver();
        } else if ("baseinfoDriverEducate".equals(method)) {
            success = timePushTask.baseinfoDriverEducate();
        } else if ("baseinfoDriverApp".equals(method)) {
            success = timePushTask.baseinfoDriverApp();
        } else if ("baseinfoDriverStat".equals(method)) {
            success = timePushTask.baseinfoDriverStat();
        } else if ("baseinfoPassenger".equals(method)) {
            success = timePushTask.baseinfoPassenger();
        } else if ("orderCreate".equals(method)) {
            success = timePushTask.orderCreate();
        } else if ("orderMatch".equals(method)) {
            success = timePushTask.orderMatch();
        } else if ("orderCancel".equals(method)) {
            success = timePushTask.orderCancel();
        } else if ("operateLogin".equals(method)) {
            success = timePushTask.operateLogin();
        } else if ("operateLogout".equals(method)) {
            success = timePushTask.operateLogout();
        } else if ("operateDepart".equals(method)) {
            success = timePushTask.operateDepart();
        } else if ("operateArrive".equals(method)) {
            success = timePushTask.operateArrive();
        } else if ("operatePay".equals(method)) {
            success = timePushTask.operatePay();
        } else if ("positionDriver".equals(method)) {
            success = timePushTask.positionDriver();
        } else if ("positionVehicle".equals(method)) {
            success = timePushTask.positionVehicle();
        } else if ("ratedPassenger".equals(method)) {
            success = timePushTask.ratedPassenger();
        } else if ("ratedPassengerComplaint".equals(method)) {
            success = timePushTask.ratedPassengerComplaint();
        } else if ("ratedDriverPunish".equals(method)) {
            success = timePushTask.ratedDriverPunish();
        } else if ("ratedDriver".equals(method)) {
            success = timePushTask.ratedDriver();
        }
        return success + "";
    }

    /**
     * 发送所有数据至监管部门
     *
     * @param method
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/sendAllDat", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String sendAllDat(@RequestParam(value = "method", required = false, defaultValue = "1") String method) {
        timePushTask.baseinfoCompany();
        timePushTask.baseinfoCompanyStat();
        timePushTask.baseinfoCompanyPay();
        timePushTask.baseinfoCompanyService();
        timePushTask.baseinfoCompanyPermit();
        timePushTask.baseinfoCompanyFare();
        timePushTask.baseinfoVehicle();
        timePushTask.baseinfoVehicleInsurance();
        timePushTask.baseinfoVehicleTotalMile();
        timePushTask.baseinfoDriver();
        timePushTask.baseinfoDriverEducate();
        timePushTask.baseinfoDriverApp();
        timePushTask.baseinfoDriverStat();
        timePushTask.baseinfoPassenger();
        timePushTask.orderCreate();
        timePushTask.orderMatch();
        timePushTask.orderCancel();
        timePushTask.operateLogin();
        timePushTask.operateLogout();
        timePushTask.operateDepart();
        timePushTask.operateArrive();
        timePushTask.operatePay();
        timePushTask.positionDriver();
        timePushTask.positionVehicle();
        timePushTask.ratedPassenger();
        timePushTask.ratedPassengerComplaint();
        timePushTask.ratedDriverPunish();
        timePushTask.ratedDriver();
        return "成功";
    }

    /**
     * 上传数据至监管部门
     *
     * @param method
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/initPushData", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String initPushData(@RequestParam(value = "method", required = false, defaultValue = "1") String method) {
        this.sendMsg();
        return "成功";
    }


    /**
     * 初始化订单数据
     *
     * @param method
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/initOrderData", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
    public String initOrderData(@RequestParam(value = "method", required = false, defaultValue = "1") String method) {
        this.initOrder();
        return "成功";
    }


    public boolean getDriverInfoV1(List<Vehicle> driverList, String carNo) {
        boolean rs = true;
        for (Vehicle vehicle : driverList) {
            if (vehicle.getVehicleNo() != null && vehicle.getVehicleNo().equals(carNo)) {
                rs = false;
                break;
            }
        }
        return rs;
    }

    public void sendMsg() {
        List<Driver> list = driverService.selectAll("");
        /*List<Vehicle> velist = vehicleService.selectAll("");

        List<Driver> rsList = new ArrayList<>();
        for (Driver bean : list) {
            if (!TextUtils.isEmpty(bean.getCarNo())) {
                boolean success = getDriverInfoV1(velist, bean.getCarNo());
                if (success) {
                    rsList.add(bean);
                }
            }
        }*/
        for (Driver bean : list) {
            //if ("18286088702".equals(bean.getPhoneNo())) {
            saveDat(bean);
            //}
        }
    }

    private void saveDat(Driver model) {
        String companyId = "5201HDCX8779";

        Date date = DateUtil.AddMonth(new Date(), -generateRandom(20));
        try {
            Vehicle query1 = new Vehicle();
            query1.setVehicleNo(model.getCarNo());
            Vehicle bean = vehicleService.selectOne("", query1);
            if (bean == null) {
                Vehicle vehicle = new Vehicle();
                vehicle.setCompanyId(companyId);
                vehicle.setAddress(model.getAreaCode());
                vehicle.setDriverPhone(model.getPhoneNo());
                vehicle.setCompanyId(model.getCompanyId());
                vehicle.setCompanyName(model.getCompanyName());
                vehicle.setVehicleNo(model.getCarNo());
                vehicle.setBrand(model.getCarBrand());
                vehicle.setModel(model.getCarBrand() + generateSalt(6));
                vehicle.setVehicleType(model.getCarType());
                vehicle.setVehicleColor(model.getCarColor());
                vehicle.setSeats(Long.parseLong(model.getCarSeats() + ""));
                vehicle.setDatetime(SystemUtil.getAllSeconds());
                vehicle.setEnabled(true);
                vehicle.setDriverOpenId(model.getOpenId());

                vehicle.setPlateColor("1");
                vehicle.setOwnerName(model.getTrueName());
                vehicle.setEngineId(generateRandom(50) + "QYJ" + generateRandom(20) + "K" + generateRandom(9));
                vehicle.setVIN("LSKGC" + generateRandom(9) + "GC" + generateRandom(20) + "KA" + generateRandom(99999));
                vehicle.setCertifyDateA(DateUtil.DatetoString(DateUtil.AddMonth(new Date(), -generateRandom(20)), DateUtil.dbyyyymmdd1));
                vehicle.setFuelType("1");
                vehicle.setEngineDisplace("2.0");
                vehicle.setTransAgency("贵阳市交通委员会");
                vehicle.setTransArea("贵阳市");
                Date date1 = DateUtil.AddMonth(new Date(), generateRandom(20));
                Date date2 = DateUtil.AddMonth(new Date(), -generateRandom(20));
                vehicle.setTransDateStart(DateUtil.DatetoString(date, DateUtil.dbyyyymmdd1));
                vehicle.setTransDateStop(DateUtil.DatetoString(date1, DateUtil.dbyyyymmdd1));
                vehicle.setCertifyDateB(DateUtil.DatetoString(date2, DateUtil.dbyyyymmdd1));
                vehicle.setFixState("1");
                vehicle.setCheckState("1");
                vehicle.setFeePrintId("00000000");
                vehicle.setGPSBrand("全球通");
                vehicle.setGPSModel("W1");
                vehicle.setGPSIMEI("14425635244");
                Date date4 = DateUtil.AddDay(date, 3 * generateRandom(20));
                vehicle.setGPSInstallDate(DateUtil.DatetoString(date4, DateUtil.dbyyyymmdd1));
                Date date3 = DateUtil.AddMonth(date, generateRandom(5));
                vehicle.setRegisterDate(DateUtil.DatetoString(date3, DateUtil.dbyyyymmdd1));
                vehicle.setCommercialType(1L);
                vehicle.setFareType("HDCX002");
                vehicle.setState(0L);
                vehicle.setFlag(1L);
                vehicle.setUpdateTime(DateUtil.DatetoString(new Date(), DateUtil.dbyyyymmdd1));
                vehicle.setUploadFlag(1);
                vehicleService.insert("", vehicle);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //插入车辆保险
        try {
            /*Vehicleinsurance query1 = new Vehicleinsurance();
            query1.setVehicleNo(model.getCarNo());
            Vehicleinsurance bean = vehicleinsuranceService.selectOne("", query1);
            if (bean == null) {*/
                Vehicleinsurance vehicleinsurance = new Vehicleinsurance();
                vehicleinsurance.setCompanyId(companyId);
                vehicleinsurance.setVehicleNo(model.getCarNo());
                vehicleinsurance.setInsurCom("中国平安财产保险股份有限公司(贵阳中心支公司)");
                vehicleinsurance.setInsurNum(generateSalt(6) + generateSalt(6) + generateSalt(6));
                vehicleinsurance.setInsurType("机动车交通事故责任强制保险");
                vehicleinsurance.setInsurCount(1500000.00);
                Date date5 = DateUtil.AddDay(date, 3 * generateRandom(99));
                Date date6 = DateUtil.AddYear(date5, 1);
                vehicleinsurance.setInsurEff(Long.parseLong(DateUtil.DatetoString(date5, DateUtil.dbyyyymmdd1)));
                vehicleinsurance.setInsurExp(Long.parseLong(DateUtil.DatetoString(date6, DateUtil.dbyyyymmdd1)));
                vehicleinsurance.setFlag(1L);
                vehicleinsurance.setUpdateTime(Long.parseLong(DateUtil.DatetoString(new Date(), DateUtil.dbyyyymmdd1)));
                vehicleinsurance.setUpdateTime2(0L);
                vehicleinsuranceService.insertUpper("", vehicleinsurance);
            //}
        } catch (Exception e) {
            e.printStackTrace();
        }
        //插入车辆里程
        try {
            /*Vehicletotalmile query1 = new Vehicletotalmile();
            query1.setVehicleNo(model.getCarNo());
            Vehicletotalmile bean = vehicletotalmileService.selectOne("", query1);
            if (bean == null) {*/
            Vehicletotalmile vehicletotalmile = new Vehicletotalmile();
            vehicletotalmile.setCompanyId(companyId);
            if (!TextUtils.isEmpty(model.getAreaCode())) {
                vehicletotalmile.setAddress(Long.parseLong(model.getAreaCode()));
            } else {
                vehicletotalmile.setAddress(0L);
            }
            vehicletotalmile.setVehicleNo(model.getCarNo());
            vehicletotalmile.setTotalMile(100.0 * generateRandom(9) * generateRandom(9) * generateRandom(9));
            vehicletotalmile.setFlag(1L);
            vehicletotalmile.setUpdateTime(Long.parseLong(DateUtil.DatetoString(new Date(), DateUtil.dbyyyymmdd1)));
            vehicletotalmile.setUpdateTime2(0L);
            vehicletotalmileService.insertUpper("", vehicletotalmile);
            //}
        } catch (Exception e) {
            e.printStackTrace();
        }
        //插入司机信息
        Date date9 = new Date();
        try {
            PlatformDriver query1 = new PlatformDriver();
            query1.setLicenseId(model.getIdCard());
            PlatformDriver bean = platformDriverService.selectOne("", query1);
            if (bean == null) {
                PlatformDriver platformDriver = new PlatformDriver();
                platformDriver.setCompanyId(companyId);
                platformDriver.setAddress(model.getAreaCode());
                platformDriver.setDriverName(model.getTrueName());
                platformDriver.setDriverPhone(model.getPhoneNo());
                if (model.getSex() != null && model.getSex().equals("女")) {
                    platformDriver.setDriverGender("2");
                } else {
                    platformDriver.setDriverGender("1");
                }
                String birth = DateUtil.getBirthFromIdCard(model.getIdCard());
                if (TextUtils.isEmpty(birth)) {
                    birth = "19870521";
                }
                platformDriver.setDriverBirthday(birth);
                platformDriver.setDriverNationality("中国");
                platformDriver.setDriverNation("汉族");
                platformDriver.setDriverContactAddress("贵阳市");
                platformDriver.setLicenseId(model.getIdCard());
                Date date7 = DateUtil.AddMonth(new Date(), -24 - generateRandom(30));
                platformDriver.setGetDriverLicenseDate(DateUtil.DatetoString(date7, DateUtil.dbyyyymmdd1));
                platformDriver.setDriverLicenseOn(DateUtil.DatetoString(date7, DateUtil.dbyyyymmdd1));
                platformDriver.setDriverLicenseOff(DateUtil.DatetoString(DateUtil.AddYear(date7, 10), DateUtil.dbyyyymmdd1));
                platformDriver.setTaxiDriver(0L);
                platformDriver.setCertificateNo(generateSalt(6) + generateSalt(6));
                platformDriver.setNetworkCarIssueOrganization("贵阳市交通委员会");
                Date date8 = DateUtil.AddDay(new Date(), -generateRandom(20) * 30);
                platformDriver.setNetworkCarIssueDate(DateUtil.DatetoString(date8, DateUtil.dbyyyymmdd1));
                platformDriver.setGetNetworkCarProofDate(DateUtil.DatetoString(date8, DateUtil.dbyyyymmdd1));
                platformDriver.setNetworkCarProofOn(DateUtil.DatetoString(date8, DateUtil.dbyyyymmdd1));
                platformDriver.setNetworkCarProofOff(DateUtil.DatetoString(DateUtil.AddYear(date8, 3), DateUtil.dbyyyymmdd1));
                platformDriver.setRegisterDate(DateUtil.DatetoString(DateUtil.AddDay(date8, generateRandom(50)), DateUtil.dbyyyymmdd1));
                platformDriver.setFullTimeDriver(0L);
                platformDriver.setInDriverBlacklist(0L);
                platformDriver.setCommercialType(1L);
                platformDriver.setContractCompany("贵州金汇达导航科技有限公司");
                date9 = DateUtil.AddDay(new Date(), -generateRandom(100) * 30);
                platformDriver.setContractOn(DateUtil.DatetoString(date9, DateUtil.dbyyyymmdd1));
                platformDriver.setContractOff(DateUtil.DatetoString(DateUtil.AddYear(date9, 2), DateUtil.dbyyyymmdd1));
                platformDriver.setState(0L);
                platformDriver.setFlag(1L);
                platformDriver.setEnabled(false);
                platformDriver.setUpdateTime(DateUtil.DatetoString(new Date(), DateUtil.dbyyyymmdd1));
                platformDriverService.insert("", platformDriver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //网约车驾驶员培训信息
        try {

           /* Drivereducate query1 = new Drivereducate();
            query1.setLicenseId(model.getIdCard());
            Drivereducate bean = drivereducateService.selectOne("", query1);
            if (bean == null) {*/
            Drivereducate drivereducate = new Drivereducate();
            drivereducate.setCompanyId(companyId);
            if (!TextUtils.isEmpty(model.getAreaCode())) {
                drivereducate.setAddress(Long.parseLong(model.getAreaCode()));
            } else {
                drivereducate.setAddress(0L);
            }
            drivereducate.setLicenseId(model.getIdCard());
            drivereducate.setCourseName("网约车从业资格证");
            drivereducate.setCourseDate(Long.parseLong(DateUtil.DatetoString(DateUtil.AddDay(date9, 10), DateUtil.dbyyyymmdd1)));
            drivereducate.setStartTime1(Long.parseLong(DateUtil.DatetoString(DateUtil.AddDay(date9, 10), DateUtil.dbyyyymmdd1)));
            drivereducate.setStopTime(Long.parseLong(DateUtil.DatetoString(DateUtil.AddDay(date9, 12), DateUtil.dbyyyymmdd1)));
            drivereducate.setDuration(48.0);
            drivereducate.setFlag(1L);
            drivereducate.setUpdateTime(Long.parseLong(DateUtil.DatetoString(new Date(), DateUtil.dbyyyymmdd1)));
            drivereducate.setUpdateTime2(0L);
            drivereducateService.insertUpper("", drivereducate);
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //驾驶员移动终端信息接口
        try {
            /*Driverapp query1 = new Driverapp();
            query1.setLicenseId(model.getIdCard());
            Driverapp bean = driverappService.selectOne("", query1);
            if (bean == null) {*/
            Driverapp driverapp = new Driverapp();
            driverapp.setCompanyId(companyId);
            if (!TextUtils.isEmpty(model.getAreaCode())) {
                driverapp.setAddress(Long.parseLong(model.getAreaCode()));
            } else {
                driverapp.setAddress(0L);
            }
            driverapp.setLicenseId(model.getIdCard());
            driverapp.setDriverPhone(model.getPhoneNo());
            driverapp.setNetType(generateRandom(2) * 1L);
            driverapp.setAppVersion("3.38");
            driverapp.setMapType(2L);
            driverapp.setState(0L);
            driverapp.setFlag(1L);
            driverapp.setUpdateTime(Long.parseLong(DateUtil.DatetoString(new Date(), DateUtil.dbyyyymmdd1)));
            driverapp.setUpdateTime2(0L);
            driverappService.insertUpper("", driverapp);
            //}
        } catch (Exception e) {
            e.printStackTrace();
        }
        //驾驶员移动终端信息接口
        try {
            /*Driverstat query1 = new Driverstat();
            query1.setLicenseId(model.getIdCard());
            Driverstat bean = driverstatService.selectOne("", query1);
            if (bean == null) {*/
            Driverstat driverstat = new Driverstat();
            driverstat.setCompanyId(companyId);
            if (!TextUtils.isEmpty(model.getAreaCode())) {
                driverstat.setAddress(Long.parseLong(model.getAreaCode()));
            } else {
                driverstat.setAddress(0L);
            }
            driverstat.setLicenseId(model.getIdCard());
            driverstat.setCycle(Long.parseLong((DateUtil.DatetoString(new Date(), DateUtil.dbyyyymmdd1))));
            driverstat.setOrderCount(0L);
            driverstat.setTrafficViolationCount(0L);
            driverstat.setComplainedCount(0L);
            driverstat.setFlag(1L);
            driverstat.setUpdateTime(Long.parseLong(DateUtil.DatetoString(new Date(), DateUtil.dbyyyymmdd1)));
            driverstat.setUpdateTime2(0L);
            driverstatService.insertUpper("", driverstat);
            // }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //驾驶员信誉
        try {
            /*RatedDriver query1 = new RatedDriver();
            query1.setLicenseId(model.getIdCard());
            RatedDriver bean = ratedDriverService.selectOne("", query1);
            if (bean == null) {*/
            RatedDriver ratedDriver = new RatedDriver();
            ratedDriver.setCompanyId(companyId);
            ratedDriver.setLicenseId(model.getIdCard());
            ratedDriver.setLevel(5L);
            ratedDriver.setTestDate(Long.parseLong(DateUtil.DatetoString(new Date(), DateUtil.dbyyyymmdd1)));
            ratedDriver.setTestDepartment("贵州金汇达导航科技有限公司");
            ratedDriver.setTestDate2(0L);
            ratedDriverService.insertUpper("", ratedDriver);
            //}
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void initOrder() {
        ServiceTrip tripQuery = new ServiceTrip();
        tripQuery.setServiceCode("01");
        List<ServiceTrip> list1 = serviceTripService.selectList("", tripQuery);

        for (ServiceTrip serviceTrip : list1) {
            if (serviceTrip.getTripNo().startsWith("KC")) {
                insertOrder(serviceTrip);
            }
        }
    }

    public void insertOrder(ServiceTrip serviceTrip) {
        /*if(serviceTrip.getTripNo().equals("KC22110314534681516")||
                serviceTrip.getTripNo().equals("KC22110312521727861")||
                serviceTrip.getTripNo().equals("KC22110310182202365")){

        }else {
            return;
        }*/
        //插入订单创建
        try {
            OrderCreate orderCreate = new OrderCreate();
            orderCreate.setCompanyId("5201HDCX8779");
            orderCreate.setAddress(520100L);
            orderCreate.setOrderId(serviceTrip.getTripNo());
            Date start = new Date(serviceTrip.getCreateTime() * 1000L);
            orderCreate.setDepartTime(Long.parseLong(DateUtil.DatetoString(start, "yyyyMMddHHmmss")));
            orderCreate.setOrderTime(Long.parseLong(DateUtil.DatetoString(start, "yyyyMMddHHmmss")));
            orderCreate.setPassengerNote(serviceTrip.getRemark());
            orderCreate.setDeparture(serviceTrip.getStartLocation());
            orderCreate.setDepLongitude(serviceTrip.getStartLon());
            orderCreate.setDepLatitude(serviceTrip.getStartLat());
            orderCreate.setDestination(serviceTrip.getEndLocation());
            orderCreate.setDestLongitude(serviceTrip.getEndLon());
            orderCreate.setDestLatitude(serviceTrip.getEndLat());
            orderCreate.setEncrypt(3L);
            orderCreate.setFareType("HDCX002");
            orderCreate.setOrderTime2(0L);
            orderCreate.setDestAddress("520100");
            User user = userService.getUserByOpenId("", serviceTrip.getOpenId());
            if(user!=null){
                orderCreate.setPassengerName(SystemUtil.desensitizedName(user.getNickName()));
                orderCreate.setPassengerPhone(SystemUtil.hidePhoneNo(user.getPhoneNo()));
                // orderCreate.setPassengerGender("");
            }
            orderCreateService.insertUpper("", orderCreate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Driver driver = driverService.getDriverByOpenId("", serviceTrip.getDriverOpenId());

        if (serviceTrip.getStatus().intValue() == 512) {
            //订单取消
            try {
                OrderCancel orderCancel = new OrderCancel();
                orderCancel.setCompanyId("5201HDCX8779");
                orderCancel.setAddress(520100L);
                orderCancel.setOrderId(serviceTrip.getTripNo());
                Date start = new Date(serviceTrip.getCreateTime() * 1000L);
                orderCancel.setOrderTime(Long.parseLong(DateUtil.DatetoString(start, "yyyyMMddHHmmss")));
                orderCancel.setCancelTime(Long.parseLong(DateUtil.DatetoString(DateUtil.AddMinute(start, generateRandom(50)), "yyyyMMddHHmmss")));
                orderCancel.setOperator("1");
                orderCancel.setCancelTypeCode("1");
                orderCancel.setCancelReason("");
                orderCancel.setCancelTime2(0L);
                orderCancelService.insertUpper("", orderCancel);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (serviceTrip.getStatus().intValue() > 1) {
            //插入订单匹配
            try {
                if (driver != null) {
                    OrderMatch orderMatch = new OrderMatch();
                    orderMatch.setCompanyId("5201HDCX8779");
                    orderMatch.setAddress(520100L);
                    orderMatch.setOrderId(serviceTrip.getTripNo());

                    orderMatch.setLongitude(driver.getLon());
                    orderMatch.setLatitude(driver.getLat());
                    orderMatch.setEncrypt(3L);
                    orderMatch.setLicenseId(driver.getIdCard());
                    orderMatch.setDriverPhone(driver.getPhoneNo());
                    orderMatch.setVehicleNo(driver.getCarNo());
                    orderMatch.setDistributeTime(Long.parseLong(DateUtil.DatetoString(new Date(), "yyyyMMddHHmmss")));
                    orderMatch.setDistributeTime2(0L);
                    orderMatchService.insertUpper("", orderMatch);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (serviceTrip.getStatus().intValue() >= 2) {
            //订单出发
            try {
                if (driver != null) {
                    OperateDepart operateDepart = new OperateDepart();
                    operateDepart.setCompanyId("5201HDCX8779");
                    operateDepart.setOrderId(serviceTrip.getTripNo());
                    operateDepart.setLicenseId(driver.getIdCard());
                    operateDepart.setFareType("HDCX002");
                    operateDepart.setVehicleNo(driver.getCarNo());
                    operateDepart.setDepLongitude(driver.getLon());
                    operateDepart.setDepLatitude(driver.getLat());
                    operateDepart.setEncrypt(3L);
                    Date destTime = new Date();
                    if (serviceTrip.getRealGoTime() != null && serviceTrip.getRealGoTime() > 0) {
                        destTime = new Date(serviceTrip.getRealGoTime() * 1000);
                    }
                    operateDepart.setDepTime(Long.parseLong(DateUtil.DatetoString(destTime, "yyyyMMddHHmmss")));
                    operateDepart.setDepTime2(0L);
                    Double mile = generateRandom(5000) * 1.0 / 1000.0;
                    operateDepart.setWaitMile(mile);
                    operateDepart.setWaitTime(mile * generateRandom(5) * 80);
                    operateDepartService.insertUpper("", operateDepart);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //订单到达
            try {
                if (driver != null) {
                    OperateArrive operateArrive = new OperateArrive();
                    operateArrive.setCompanyId("5201HDCX8779");
                    operateArrive.setOrderId(serviceTrip.getTripNo());
                    operateArrive.setDestLongitude(driver.getLon());
                    operateArrive.setDestLatitude(driver.getLat());
                    operateArrive.setEncrypt(2L);
                    Date destTime = new Date();
                    if (serviceTrip.getCompleteTime() != null && serviceTrip.getCompleteTime() > 0) {
                        destTime = new Date(serviceTrip.getCompleteTime() * 1000);
                    }
                    operateArrive.setDestTime(Long.parseLong(DateUtil.DatetoString(destTime, "yyyyMMddHHmmss")));
                    operateArrive.setDriveMile(serviceTrip.getDistance() / 1000);
                    operateArrive.setDriveTime(serviceTrip.getDuration());
                    operateArrive.setDestTime2(0L);
                    operateArriveService.insertUpper("", operateArrive);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (serviceTrip.getStatus().intValue() > 64) {
            try {
                if (driver != null) {
                    OperatePay operatePay = new OperatePay();
                    operatePay.setCompanyId("5201HDCX8779");
                    operatePay.setOrderId(serviceTrip.getTripNo());
                    operatePay.setOnArea(520100L);
                    operatePay.setDriverName(driver.getTrueName());
                    operatePay.setLicenseId(driver.getIdCard());
                    operatePay.setFareType("HDCX002");
                    operatePay.setVehicleNo(driver.getCarNo());
                    Date date = new Date(serviceTrip.getGoTime() * 1000);
                    operatePay.setBookDepTime(Long.parseLong(DateUtil.DatetoString(date, "yyyyMMddHHmmss")));
                    operatePay.setDepLongitude(serviceTrip.getStartLon());
                    operatePay.setDepLatitude(serviceTrip.getStartLat());
                    operatePay.setDepArea(serviceTrip.getStartLocation());
                    Date date1 = new Date(serviceTrip.getRealGoTime() * 1000);
                    operatePay.setDepTime(Long.parseLong(DateUtil.DatetoString(date1, "yyyyMMddHHmmss")));
                    operatePay.setDestLongitude(serviceTrip.getEndLon());
                    operatePay.setDestLatitude(serviceTrip.getEndLat());
                    operatePay.setDestArea(serviceTrip.getEndLocation());
                    operatePay.setDestTime(Long.parseLong(DateUtil.DatetoString(new Date(), "yyyyMMddHHmmss")));
                    //operatePay.setBookModel("");
                    // operatePay.setModel("");
                    operatePay.setDriveMile(serviceTrip.getDistance() / 1000);
                    operatePay.setDriveTime(serviceTrip.getDuration());
                    OperateDepart query1 = new OperateDepart();
                    query1.setOrderId(serviceTrip.getTripNo());
                    OperateDepart operateDepart = operateDepartService.selectOne("", query1);
                    if (operateDepart != null) {
                        operatePay.setWaitMile(operateDepart.getWaitMile());
                    }
                    operatePay.setFactPrice(serviceTrip.getPayFee());
                    operatePay.setPrice(serviceTrip.getPayFee());
                    operatePay.setCashPrice(0.0D);
                    operatePay.setLineName(PayType.getPayName(serviceTrip.getPayId()));
                    operatePay.setLinePrice(serviceTrip.getPayFee());
                    operatePay.setPosName("");
                    operatePay.setPosPrice(0.0D);
                    operatePay.setBenfitPrice(0.0D);
                    operatePay.setBookTip(0.0D);
                    operatePay.setPassengerTip(0.0D);
                    operatePay.setPeakUpPrice(0.0D);
                    operatePay.setNightUpPrice(0.0D);
                    operatePay.setFarUpPrice(0.0D);
                    operatePay.setOtherUpPrice(0.0D);
                    operatePay.setPayState("1");
                    Date datepay = new Date(serviceTrip.getPayTime() * 1000);
                    operatePay.setPayTime(Long.parseLong(DateUtil.DatetoString(datepay, "yyyyMMddHHmmss")));
                    Date datecompate = new Date(serviceTrip.getCompleteTime() * 1000);
                    operatePay.setOrderMatchTime(Long.parseLong(DateUtil.DatetoString(datecompate, "yyyyMMddHHmmss")));
                    operatePay.setInvoiceStatus("0");
                    operatePay.setOrderMatchTime2(0L);
                    operatePayService.insertUpper("", operatePay);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
